import { URL_WAVE_AMOUNT } from "@/comm/api";
import { formatKey } from "@/comm/format";
import { get } from "@/comm/request";
import { sortList } from "@/comm/tooler";
import { BIGTRADE, Bus } from "@/connetion/Bus";
import { defineStore } from "pinia";
interface IState {
    NEW: Array<any>;
    HOT: Array<any>;
    UP: Array<any>;
    DOWN: Array<any>;
    TURNOVER: Array<any>;
    VOLATILITY: Array<any>;
    REAL_TIME_BIG_TURNOVER: Array<any>;
    REAL_TIME_BIG_TURNOVER_MORE:Array<any>;
    WAVE: Array<any>;
    WAVE_M5: Array<any>;
}

export const useRank = defineStore("rank", {
    state: (): IState => ({
        NEW: [],
        HOT: [],
        UP: [],
        DOWN: [],
        TURNOVER: [],
        VOLATILITY: [],
        REAL_TIME_BIG_TURNOVER: [],
        REAL_TIME_BIG_TURNOVER_MORE:[],
        WAVE: [],
        WAVE_M5: []
    }),

    getters: {},
    actions: {
        changeRanks(val: any) {
            if (val.data) {
                val.data.forEach((i: any) => {
                    i.key = formatKey(i.E);
                });
            }
            // console.log("gourp=========", val.group)
            if (val.group == "CAPITAL_ABNORMAL") {
                console.log("推送交易", val);

            }
            if (val.group == "REAL_TIME_BIG_TURNOVER") {
                // console.log(val.group , "REAL_TIME_BIG_TURNOVER")
                console.log('大额成交推送',val.data)
                this.changeBIG_TURNOVER_MORE(val.data)
                this.changeBIG_TURNOVER(val.data)
                Bus.emit(BIGTRADE, val.data);
            }
            else {
                (this as any)[val.group] = val.data;
            }

            // console.log("--- 榜單", val.group);
        },
        updateWave(time: string) {
            get(URL_WAVE_AMOUNT, {
                type: time,
                limit: 100
            }).then((res: any) => {
                console.log("tom", res.data);
                if (res.data?.length) {
                    let ary = res.data;
                    // sortList(ary, "volatility", true);
                    this.WAVE = ary;
                    // list.value = ary.slice(0, limit);
                }
            });
        },
        resetWave(ary: any) {
            this.WAVE = ary;
        },
        resetWaveM5(ary: any) {
            this.WAVE_M5 = ary;
        },
        changeBIG_TURNOVER_MORE(val: any) {
            let arr = this.REAL_TIME_BIG_TURNOVER_MORE;
            val.forEach((i: any) => {
                if (arr.find(item => item.id == i.id)) {
                    console.log("has big amount")
                }
                else {
                    arr.push(i);
                }
            })
            sortList(arr, "T");
            this.REAL_TIME_BIG_TURNOVER_MORE = arr
        },
        changeBIG_TURNOVER(val: any) {
            let arr = this.REAL_TIME_BIG_TURNOVER;
            val.forEach((i: any) => {
                if (arr.find(item => item.id == i.id)) {
                    console.log("has big amount")
                }
                else {
                    arr.push(i);
                }
            })
            sortList(arr, "T");
            this.REAL_TIME_BIG_TURNOVER = arr.slice(0, 10);

            // if(this.REAL_TIME_BIG_TURNOVER.length > 20){
            //     this.REAL_TIME_BIG_TURNOVER = this.REAL_TIME_BIG_TURNOVER.slice(0, 20)
            // }
        }
    }
});
